package ru.timofeyco.justfeelbetter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import ru.timofeyco.justfeelbetter.db.JustFeelBetterDatabase;
import ru.timofeyco.justfeelbetter.model.DayDrillTypeState;
import ru.timofeyco.justfeelbetter.model.DayDrills;
import ru.timofeyco.justfeelbetter.model.DayResult;
import ru.timofeyco.justfeelbetter.model.Drill;
import ru.timofeyco.justfeelbetter.model.DrillType;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;

public class DayActivity extends Activity {	
	private static final int ADD_DRILL_MENU_ITEM = 1;
	private static final int PREFERENCES_MENU_ITEM = 2;
	
	JustFeelBetterDatabase dbHelper;
	DayResultView dayResultView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dayResultView = new DayResultView(this);
        Display display = getWindowManager().getDefaultDisplay();
        dayResultView.setScreenBounds(new Rect(0, 0, display.getWidth(), display.getHeight()));
             
        dbHelper = new JustFeelBetterDatabase(this);
        updateDayResultView();
        setContentView(dayResultView);
    }
    
    private void updateDayResultView() {
    	DayDrills dayDrills = new DayDrills();
        Date currDate = Calendar.getInstance().getTime();
        Date currDay = new Date(currDate.getYear(), currDate.getMonth(), currDate.getDate());
        dayDrills.setDate(currDay);
        
        Drill[] drills = dbHelper.getDayDrills(currDay);        
        int productivityPercent = DayResultView.calculatePercent(new ArrayList<Drill>(Arrays.asList(drills)));
        dayResultView.setProductivityPercent(productivityPercent);
        ArrayList<DayDrillTypeState> drillTypeStateList = createDrillTypeStateList(drills);
        dayResultView.setDayDrillTypeState(drillTypeStateList);
        
        dbHelper.updateDayResult(new DayResult(currDay, productivityPercent));
    }
    
    private ArrayList<DayDrillTypeState> createDrillTypeStateList(Drill[] drills) {
    	HashMap<DrillType, Integer> map = new HashMap<DrillType, Integer>();
    	for (Drill drill: drills) {
    		if (!map.containsKey(drill.getType())) {
    			map.put(drill.getType(), drill.getCount());
    		} else {
    			Integer count = map.get(drill.getType());
    			map.put(drill.getType(), count + drill.getCount()); 
    		}
    	}
    	
    	ArrayList<DayDrillTypeState> resultList = new ArrayList<DayDrillTypeState>();
    	for (DrillType drillType: map.keySet()) {
    		resultList.add(new DayDrillTypeState(drillType, map.get(drillType)));
    	}
    	return resultList;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
      super.onCreateOptionsMenu(menu);

      /*
        MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
       * */
      //MenuItem item = menu.add (0, 1, ADD_DRILL_MENU_ITEM, getString(R.string.add_drill_menu_item));
      menu.add(0, ADD_DRILL_MENU_ITEM, 1, getString(R.string.add_drill_menu_item));
      menu.add(1, PREFERENCES_MENU_ITEM, 2, getString(R.string.pref_title));
      
      return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem)  {
    	int itemId = menuItem.getItemId();
    	switch (itemId) {
		case ADD_DRILL_MENU_ITEM:
			// show add drill activity
			Intent addDrillIntent =  new Intent(this, AddDrillActivity.class);
			this.startActivityForResult(addDrillIntent, RESULT_OK);			
			break;
			
		case PREFERENCES_MENU_ITEM:
			Intent preferencesIntent =  new Intent(this, PreferencesActivity.class);
			this.startActivityForResult(preferencesIntent, RESULT_OK);
			break;

		default:
			break;
		}
    	return true;	
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	updateDayResultView();
    }
}
